#include "qglobalobject.h"

#include "quixml.h"
#include "qdatamanager.h"
#include "qwidgetfactory.h"
#include "qproject.h"
#include "qobjecthost.h"

QGlobalObject* QGlobalObject::m_object=NULL;

QGlobalObject::QGlobalObject(QObject *parent) :
    QObject(parent),
    m_zValue(0)
{
}

QGlobalObject::~QGlobalObject()
{
    qDeleteAll(m_pages);
    m_pages.clear();
}

QGlobalObject* QGlobalObject::globalObject()
{
    if(m_object==NULL)
    {
        m_object=new QGlobalObject;
    }
    return m_object;
}

void QGlobalObject::releaseGlobalObject()
{
    if(m_object!=NULL)
    {
        delete m_object;
        m_object=NULL;
    }
}

QWidget* QGlobalObject::createWidget(const QString &code)
{
    QWidget* wid=NULL;
    XMLWidget   xml;
    QXmlStreamReader r(code);
    while(!r.atEnd())
    {
        if(r.readNext()==QXmlStreamReader::StartElement)
        {
            if(r.name()=="widget")
            {
                xml.read(r);
            }
        }
    }
    if(r.hasError())
    {
        return NULL;
    }

    QList<XMLProperty*> list;

    list=xml.propertys();
    wid=WidgetFactory::create("QWidget");
    if(wid==NULL)
    {
        return NULL;
    }
    wid->setProperty("running",true);
    connect(this,SIGNAL(initProperty()),wid,SLOT(initProperty()));
    connect(this,SIGNAL(initGlobal()),(QObjectHost*)((wid->property("host").toInt())),SLOT(initGlobalObject()));
    foreach(XMLProperty* p,list)
    {
        wid->setProperty(p->propertyName().toLocal8Bit(),p->value());
    }
    QList<XMLWidget*>   widList;
    widList=xml.children();
    QWidget* c;
    QString cla;
    QList<QWidget*> zlist;
    int index;
    foreach(XMLWidget* w,widList)
    {
        cla=w->attribute("class","");
        if(cla!="")
        {
            c=WidgetFactory::create(cla);
            if(c!=NULL)
            {
                c->setParent(wid);
                c->setProperty("running",true);
                connect(this,SIGNAL(initProperty()),c,SLOT(initProperty()));
                connect(this,SIGNAL(initGlobal()),(QObjectHost*)((c->property("host").toInt())),SLOT(initGlobalObject()));
                list=w->propertys();
                c->setVisible(true);
                foreach(XMLProperty* p,list)
                {
                    c->setProperty(p->propertyName().toLocal8Bit(),p->value());
                }
            }
        }
        for(index=0;index<zlist.size();index++)
        {
            if(zlist.at(index)->property("Z-Value").toInt()>c->property("Z-Value").toInt())
            {
                break;
            }
        }
        zlist.insert(index,c);
    }

    foreach(QWidget* ww,zlist)
    {
        ww->raise();
    }
    return wid;
}

void QGlobalObject::showPage(const QString &page)
{
    if(m_pages.contains(page))
    {
        QGraphicsProxyWidget *wid=m_pages.value(page);
        wid->setVisible(true);
        m_zValue++;
        wid->setZValue(m_zValue);

    }
    else
    {
        if(createPage(page))
        {
            showPage(page);
        }
    }
}

bool QGlobalObject::createPage(const QString &page)
{
    tagPageInfo info=QProject::project()->getPage(page);

    if(info.m_name==page)
    {
        QWidget* wid=createWidget(info.m_data);
        if(wid!=NULL)
        {
            QGraphicsProxyWidget *w=m_view->scene()->addWidget(wid);
            m_pages.insert(page,w);
            emit initProperty();
            return true;
        }
    }
    return false;
}

void QGlobalObject::hidePage(const QString &page)
{
    QGraphicsProxyWidget *wid=m_pages.value(page,NULL);
    if(wid!=NULL)
    {
        wid->setVisible(false);
    }
}

void QGlobalObject::close()
{
    m_view->parentWidget()->close();
}

void QGlobalObject::setMemeryData(const QString &key, const QVariant &value)
{
    QDataManager::manager()->setUserData(key,value);
}

QVariant QGlobalObject::memeryData(const QString &key)
{
    return QDataManager::manager()->userData(key,"");
}

void QGlobalObject::setView(QGraphicsView *view)
{
    m_view=view;
}

QMap<QString,QGraphicsProxyWidget*>  QGlobalObject::pages()
{
    return m_pages;
}

void QGlobalObject::updateGlobal()
{
    emit initGlobal();
}
